<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script src="./js/is_object.js"></script>
  <script>
    // let map = new WeakMap()

    function deepCopy(originValue, map = new WeakMap()) {
      // 如果是symbol类型
      if (originValue instanceof Symbol) return Symbol(originValue.description)

      // 如果不是对象,直接返回
      if (!isObject(originValue)) return originValue

      // 如果是set类型
      if (originValue instanceof Set) {
        const newSet = new Set()
        for (const setItem of originValue) {
          newSet.add(deepCopy(setItem))
        }
      }

      // 如果是函数,不需要深拷贝
      if (originValue instanceof Function) return originValue

      if (map.get(originValue)) return map.get(originValue)
      const newObj = Array.isArray(originValue) ? [] : {}
      map.set(originValue, newObj)
      // 遍历普通的key
      for (const key in originValue) {
        newObj[key] = deepCopy(originValue[key], map)
      }

      //遍历Symbol类型的key
      const symbolKeys = Object.getOwnPropertySymbols(originValue)
      for (const symbolKey of symbolKeys) {
        newObj[Symbol(symbolKey.description)] = deepCopy(originValue[symbolKey], map)
      }
      return newObj
    }

    const info = {
      name: "why",
      age: 18,
      friend: {
        name: "kobe",
        address: {
          name: "洛杉矶",
          detail: "斯坦普斯中心"
        }
      },
      // self:info
    }
    info.self = info

    let newObj = deepCopy(info)
    console.log(newObj)
  </script>
</body>

</html>